//=require shipper_register
$(document).ready(function () {

    if ($('#resultTap').val() == 'basic') {
        basic_edit();
    } else if ($('#resultTap').val() == 'password') {
        password();
    }

    // 新增页面,单击确定
    $('#u_n_s').click(function () {
        var login_flag = check_login($("#user_login"));
        if (login_flag == undefined)
            login_flag = ($("#user_login").next().val() == "true") ? true : false;
        var password_flag = check_password($("#user_password").val());
        var confirm_flag = check_password_confirmation($("#user_password").val(), $("#user_password_confirmation").val());
        var email_flag = check_email($("#user_email"));
        if (email_flag == undefined)
            email_flag = ($("#user_email").next().val() == "true") ? true : false;
        var flag = login_flag && password_flag && confirm_flag && email_flag;
        if (flag)
            $('form')[0].submit();
        return false;
    });

    // 基础编辑页面,单击确定
    $('#u_basic_sure').click(function () {
        var email_flag = check_email($("#user_email"));
//        var old_email
        if (email_flag == undefined)
            email_flag = ($("#user_email").next().val() == "true") ? true : false;
        if(email_flag)
            $('#basic_form').submit();
        return false;
    });

    // 密码编辑页面,单击确定
    $('#u_password_sure').click(function () {
        console.debug($('#user_password').attr("value"));
        if (!$('#user_password').attr("value")) {
            show_notice("密码不能为空");
            return;
        }
        if (!$('#user_password_confirmation').attr("value")) {
            show_notice("密码确认不能为空");
            return;
        }
        $('#password_form').submit();
        return false;
    });

    // tab页切换
    $('#b_i').click(function () {
        basic_edit();
        return false;
    });

    $('#c_p').click(function () {
        password();
        return false;
    });

    //========new page===============
    $("#user_login").focusout(function() {
        check_login($(this));
    });
    $("#user_password").focusout(function() {
        check_password($(this).val());
    });
    $("#user_password_confirmation").focusout(function() {
        check_password_confirmation($("#user_password").val(), $(this).val());
    });
    $("#user_email").focusout(function() {
        check_email($(this));
    });
});

// 基本资料编辑
function basic_edit() {
    $(".tab-selected").removeClass("tab-selected");
    $('#b_i').addClass('tab-selected');
//    $('#c_p').removeClass('tab-selected');
    $('#edit_basic_info').show();
    $('#change_password').hide();
}

// 更改密码
function password() {
    $(".tab-selected").removeClass("tab-selected");
    $('#c_p').addClass('tab-selected');
//    $('#b_i').removeClass('tab-selected');
    $('#edit_basic_info').hide();
    $('#change_password').show();
}

//查找
function search_user() {
    var login = $("#login_name").val().trim();
    if ($("#user_role_id").length == 1)
        var role = $("#user_role_id").val().trim();
    var params = "";
    if (login != "")
        params = params + "login=" + login + "&";
    if (role != "" && role != undefined )
        params = params + "user_role_id=" + role;
    window.location.href = "/users?" + params;
}